package com.zhipu.balance.utils.request

import android.text.TextUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPUtils
import com.zhipu.balance.listener.AddOnClickListener
import com.zhipu.balance.mobile.ResultEntity
import com.zhipu.balance.utils.InitializeUtils
import com.zhy.http.okhttp.OkHttpUtils
import com.zhy.http.okhttp.callback.StringCallback
import okhttp3.Call
import okhttp3.MediaType

/**
 * 接口请求
 */
object InterfaceRequestUtils {

    /**
     * {"uniqueId" : "hz015653569317"}
     */
    fun getUniqueInfoById(uniqueId : String,mAddOnClickListener : AddOnClickListener){
        if(TextUtils.isEmpty(uniqueId)){
            mAddOnClickListener.onSkipClicked("")
            return
        }
        OkHttpUtils.postString()
            .url(SPUtils.getInstance().getString("NetworkSettings","https://cg.zhipu-china.com/") + "/getUniqueInfoById")
            .content("{\"uniqueId\" : \"$uniqueId\"}")
            .mediaType(MediaType.parse("application/json; charset=utf-8")).build().execute(object : StringCallback() {
                override fun onError(call: Call?, e: Exception?, id: Int) {
                    mAddOnClickListener.onSkipClicked("")
                    println(e?.message)
                }
                override fun onResponse(response: String?, id: Int) {
                    var mResultEntity = GsonUtils.fromJson(response,ResultEntity::class.java)
                    when(mResultEntity?.status){
                        200 ->{
                            mAddOnClickListener.onSkipClicked(mResultEntity.message.toString())
                        }
                        else ->{
                            InitializeUtils.getDefaultMaker()?.show(mResultEntity.msg)
                        }
                    }
                }
            })
    }
}